<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>defineProperty</title>
</head>
<body>
  <input type="text" id="inpText">
  <div class="content"></div>
  <script>
    /* 实现一个最简单的双向绑定 */
    const content = document.querySelector('.content')
    const data = {
      name: ''
    }
    inpText.addEventListener('input', function() {
      data.name = this.value
    })
    let value // 临时变量
    Object.defineProperty(data, 'name', {
      get() {
        return value
      },
      set(newValue) {
        if (value === newValue) return
        value = newValue
        content.innerHTML = value
        console.log('data.name=>' + data.name)
      }
    })
  </script>
</body>
</html>
